package com.lb.activity.rest;

import com.lb.activity.po.ExaminePermission;
import com.lb.activity.po.ExamineRole;
import com.lb.activity.service.ExamineRoleService;
import com.lb.activity.util.PermissionTree;
import com.lb.activity.util.ResultUtil;
import com.lb.activity.vo.RoleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author liubin
 */
@Api("审批角色")
@RestController
@RequestMapping("examine/role")
@Slf4j
public class ExamineRoleController {

    @Autowired
    private ExamineRoleService roleService;

    @GetMapping("toRole")
    @ApiOperation(value = "跳转到role页面")
    public ModelAndView toRole(){
        return new ModelAndView("examine/role/role");
    }

    @GetMapping("toAdd")
    @ApiOperation(value = "跳转到角色添加页面")
    public ModelAndView toAdd(){
        return new ModelAndView("examine/role/role-add");
    }

    @GetMapping("list")
    @ApiOperation(value = "分页查询角色",notes = "分页查询")
    public ResultUtil list(RoleVo roleVo){
        ResultUtil resultUtil =  roleService.list(roleVo);
        return resultUtil;
    }

    @PostMapping("add")
    @ApiOperation(value = "添加角色")
    public ResultUtil add(ExamineRole role){
        ResultUtil resultUtil = roleService.add(role);
        return resultUtil;
    }

    @GetMapping("toUpdate")
    @ApiOperation(value = "跳转到更新页面",notes = "传递id，查询角色返回")
    public ModelAndView toUpdate(Long id){
        ModelAndView modelAndView = new ModelAndView();
        ExamineRole role = roleService.getOne(id);
        modelAndView.setViewName("examine/role/role-update");
        modelAndView.addObject("role",role);
        return modelAndView;
    }

    @PostMapping("update")
    @ApiOperation(value = "更新角色")
    public ResultUtil update(ExamineRole role){
        ResultUtil resultUtil = roleService.update(role);
        return resultUtil;
    }

    @PostMapping("delete")
    @ApiOperation(value = "删除角色")
    public ResultUtil delete(Long id){
        ResultUtil resultUtil = roleService.delete(id);
        return resultUtil;
    }

    @PostMapping("changeStatus")
    @ApiOperation(value = "修改角色的状态")
    public ResultUtil changeStatus(Long id,boolean boolStatus){
        ResultUtil resultUtil = roleService.changeStatus(id,boolStatus);
        return resultUtil;
    }

    @GetMapping("assignRole")
    @ApiOperation(value = "分配角色",notes = "传递roleId")
    public ModelAndView assignRole( Long id){
        //查询role信息，反显
        ExamineRole role = roleService.getOne(id);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("role",role);
        modelAndView.setViewName("examine/role/assignRole");
        return modelAndView;
    }

    @PostMapping("menuTree")
    @ApiOperation(value = "获取角色树")
    public PermissionTree<ExaminePermission> roleTree(Long roleId) {
        PermissionTree<ExaminePermission> tree = roleService.getRoleTree(roleId);
        return tree;
    }

    @PostMapping("modifyRoleMenus")
    @ApiOperation(value = "分配角色的权限")
    public ResultUtil modifyRoleMenus(Long id,String[] menuIds){
        try {
            roleService.modifyRoleMenus(id,menuIds);
        }catch (Exception e){
            log.error(e.getMessage());
            return ResultUtil.build(400,"分配失败");
        }
        return ResultUtil.ok();
    }

    @GetMapping("allRoles")
    @ApiOperation(value = "全部审批权限")
    public ResultUtil allRoles(){
        ResultUtil resultUtil = roleService.allRoles();
        return resultUtil;
    }

}
